package com.xiaolin.designmode.iterator;

import java.util.List;

/**
 * @ClassName ConcreteIterator
 * @Description 具体迭代器
 * @Detail detail
 * @Author MyPC
 * @Date 2020/11/8
 * @Version 1.0
 */
public class ConcreteIterator implements Iterator {
    private List<Object> list;
    private int index=-1;
    public ConcreteIterator(List<Object> list){
        this.list=list;
    }

    /**
     * 获取第一个元素
     * @return
     */
    @Override
    public Object first() {
        index =0;
        if(list.size()>index){
            //有元素
            return list.get(index);
        }
       return null;
    }

    /**
     * 获取下一个元素
     * @return
     */
    @Override
    public Object next() {
        if(++index < list.size()){
            return list.get(index);
        }else {
            return null;
        }
    }

    /**
     * 判断是否有下一个元素
     * @return
     */
    @Override
    public boolean hasNext() {
        return index < list.size()-1;
    }
}
